#ifndef MYDATABASE_H
#define MYDATABASE_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QStringList>

#define VERSION  1
#define DataBaseVersion 20200608


class MyDataBase : public QObject
{
    Q_OBJECT
public: 
    const static QString TAB_VERSION;
    const static QString TAB_STRUCTURE;

    const static QString _ID;
    const static QString _TAB_VERNUM;
    const static QString _TAB_VERVALUE;
    const static QString _TAB_COMPONENT_NAME;
    const static QString _TAB_COMPONENT_TYPE;
    const static QString _TAB_COMPONENT_BELONG;


    static QSqlDatabase& getDataBase();
    static MyDataBase* getDataBasePtr();
    static void createVersionTable(QSqlDatabase &db);
    static void createStructureTable(QSqlDatabase &db);

    static qint64 getDataBaseVersion(QSqlDatabase &db);
    static void setDataBaseVersion(qint64 value, QSqlDatabase &db);

    static void initStructureTable(QSqlDatabase &db);

signals:

private:
    explicit MyDataBase(QObject *parent = nullptr);
    static MyDataBase *mDbInstance;
    QSqlDatabase mDb;

};

class DataBaseQuery : public QSqlQuery
{
public:
    explicit DataBaseQuery(QSqlDatabase db = MyDataBase::getDataBase());
    bool containColumn(QString tabName, QString cloumn);
    bool addColumn(QString tabName, QString column, QString option);
    bool insert(QString tabName, QMap<QString,QVariant> &dataMap);
    bool selectTable(QString tabName, const QStringList *keyList=NULL, QString option="", QString shortItem="", bool shortdesc=0);
    bool selectTable(QStringList *tabList, const QStringList *keyList=NULL, QString option="");
    bool execString(const QString &str);
    bool resetTable(QString tabName);
    bool resetAllTable();
    int size();
    bool isItemExist(const QString &tabName, const QString &item, const QVariant &content);
    bool updateItem(QString tabName, QString fieldName, QString now, QMap<QString, QVariant> &dataMap_new);
    bool updateItem(QString tabName, QMap<QString, QVariant> &dataMap, QString option="");
private:

};

#endif // MYDATABASE_H
